package com.athuahua.spzx.controller.system;

import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.SysUser;
import com.athuahua.spzx.entity.vo.GeneralVo;
import com.athuahua.spzx.dto.system.AssginRoleDto;
import com.athuahua.spzx.dto.system.SysUserDto;
import com.athuahua.spzx.service.SysUserRoleService;
import com.athuahua.spzx.service.SysUserService;
import com.athuahua.spzx.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: huahua
 * @Date: 2024/7/23 0023
 * @Time: 0:59
 * @Description:
 */
@Tag(name = "用户管理接口")
@RestController
@RequestMapping("admin/system/sysUser")
@Slf4j
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @GetMapping("findByPage/{page}/{size}")
    @Operation(summary = "分页查询用户列表")
    public Result findByPage(@PathVariable("page") int page,
                             @PathVariable("size") int size,
                             SysUserDto sysUserDto) {
        log.info("findByPage page:{}, size:{}, sysUserDto:{}", page, size, sysUserDto);

        IPage<SysUser> list = sysUserService.findByPage(page, size, sysUserDto);


        GeneralVo<SysUser> sysUserVo = new GeneralVo<>();
        sysUserVo.setList(list.getRecords());
        sysUserVo.setTotal((int) list.getTotal());


        return Result.success(sysUserVo);
    }

    @PutMapping("updateSysUser")
    @Operation(summary = "更新用户信息")
    @AutoLog(title = "用户管理模块修改", businessType = 2) //记录日志使用
    public Result updateSysUser(@RequestBody SysUser sysUser) {
        log.info("updateSysUser sysUser:{}", sysUser);
        sysUserService.updateById(sysUser);
        return Result.success(null);
    }

    @PostMapping("saveSysUser")
    @Operation(summary = "保存用户信息")
    @AutoLog(title = "用户管理模块新增", businessType = 1) //记录日志使用
    public Result saveSysUser(@RequestBody SysUser sysUser) {
        log.info("saveSysUser sysUser:{}", sysUser);
        sysUserService.save(sysUser);
        return Result.success(null);
    }

    //deleteById
    @DeleteMapping("deleteById/{id}")
    @Operation(summary = "删除用户信息")
    @AutoLog(title = "用户管理模块删除", businessType = 3) //记录日志使用
    public Result deleteSysUser(@PathVariable("id") String id) {
        log.info("deleteSysUser id:{}", id);
        sysUserService.removeById(id);
        return Result.success(null);
    }


    //doAssign

    @PostMapping("doAssign")
    @Operation(summary = "分配角色")
    @AutoLog(title = "用户管理模块分配角色", businessType = 2) //记录日志使用
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto) {
        log.info("doAssign assginRoleDto:{}", assginRoleDto);
        sysUserRoleService.doAssign(assginRoleDto);
        return null;
    }

}
